package com.drayge.dotcircle;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;

@SuppressLint("NewApi")
public class DotCircularRingView extends View {
	
	private int totalPoints = 60;
	private int pieceDegree;
	private int mRadius = 0;
	private int mRingRadius = 10;
	private int mSmallRingRadius = 5;
	
	private int percent = 75;
	private ValueAnimator valueAnimator;
	private Interpolator interpolator = new AccelerateDecelerateInterpolator();
	private int colorPoints = 0;
	private int color_grey;
	private int color_highlight;
	private float diameter;
	/**
	 * 画笔
	 */
	private Paint mPaint;
	public DotCircularRingView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public DotCircularRingView(Context context) {
		this(context, null);
	}
	
	public DotCircularRingView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		color_grey = Color.parseColor("#E6E6E6");
		
		TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DotCircularRingView,
				defStyle, 0);
		color_highlight = typedArray.getColor(R.styleable.DotCircularRingView_highlightColor, Color.parseColor("#DF4D4D"));
		diameter = typedArray.getDimension(R.styleable.DotCircularRingView_diameter, 0);
		percent = typedArray.getInt(R.styleable.DotCircularRingView_progress, 0);
		typedArray.recycle();
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		pieceDegree = 360 / totalPoints;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if(diameter == 0) {
			diameter = getWidth();
		}
		mRadius = ((int)diameter >> 1) - mRingRadius;
		int angle = 0;
		
		for(int i = 0; i <= totalPoints; i ++) {
			int x = (int) (((int)diameter >> 1) + (Math.sin(Math.toRadians(angle) ) * mRadius)) + ((getWidth()>>1) - ((int)diameter>>1));
			int y = (int) (((int)diameter >> 1) - (Math.cos(Math.toRadians(angle) ) * mRadius)) + ((getHeight()>>1) - ((int)diameter>>1));
			
			//Log.d("test", "x: " + x + ",y: " + y);
			if(i > 0 && i <= colorPoints) {
				mPaint.setColor(color_highlight);
			} else {
				mPaint.setColor(color_grey);
			}
			
			if((angle % 30) == 0) { //画大圆
				canvas.drawCircle(x, y, mRingRadius, mPaint);
				mPaint.setColor(Color.WHITE);
				canvas.drawCircle(x, y, mRingRadius-4, mPaint);
			} else {
				
				canvas.drawCircle(x, y, mSmallRingRadius, mPaint);
			}
			angle += pieceDegree;
		}
		if(null == valueAnimator) {
			initValueAnimator();
		}
	}
	
	public int getColorPoints() {
		return colorPoints;
	}

	public void setColorPoints(int colorPoints) {
		this.colorPoints = colorPoints;
	}

	private void initValueAnimator() {
        valueAnimator = new ValueAnimator();
        valueAnimator.setInterpolator(interpolator);
        setValue(percent);
    }
	
	public void setValue(int percent) {
		int colorPoints = (int) (totalPoints * (percent / (float)100));
		valueAnimator.setIntValues(0, colorPoints);
		valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				//获得当前动画的进度值，整型
	            int currentValue = (Integer)valueAnimator.getAnimatedValue();
	            setColorPoints(currentValue);
	            postInvalidate();
			}
		});
        valueAnimator.setDuration(1000).start();
	}
}
